
*** Credit spreads
* CDS-bond basis
tempfile cds_bond_basis
import delimited "./DataSkeleton/AssetClasses/PC1/bond_cds_07122021.csv", varnames(1)  clear
gen t=date(date,"MDY")
drop date
rename t date
order date
format date %td
destring _all, force replace
*rename highyieldallcdsbondbasis cds_bond_hy
rename cdsbondbasishgtradedcdsbondbasis cds_bond
keep cds_bond date
save `cds_bond_basis',replace

* CDX basis
tempfile cdx_basis
import delimited "./DataSkeleton/AssetClasses/PC1/cdx_cds_07122021.csv", varnames(1)  clear
gen t=date(date,"MDY")
drop date
rename t date
order date
format date %td
destring _all, force replace
rename djcdxnaigmainontherun5yrbasistot cdx_basis
keep cdx date 
save `cdx_basis',replace

** JPM tenor basis
tempfile tenor_basis
import delimited "./DataSkeleton/AssetClasses/PC1/tenor_basis_07122021.csv", varnames(1)  clear
gen t=date(date,"MDY")
drop date
rename t date
order date
format date %td
destring _all, force replace
rename month tenor_basis
keep tenor_basis date 
save `tenor_basis',replace


* 30-year swap spread
tempfile swap_spread
import excel "./DataSkeleton/AssetClasses/PC1/raw_arb_basis.xlsx",sheet("value") firstrow clear
keep USSP30 date
rename USSP30 swap_spread
label var swap_spread "30Y swap-Tresaury spread"
sort date
destring swap_spread, force replace
save `swap_spread',replace


* Refcorp-Treasury
tempfile refco_treas
import excel "./DataSkeleton/AssetClasses/PC1/raw_arb_basis.xlsx",sheet("value") firstrow clear
destring C* G* US*, force replace
gen refco_treas=100*(C0915Y-C0825Y)
keep date refco_treas 
label var refco_treas "5Y Refcorp-Treasury spread"
save `refco_treas',replace

* KfW-Bund
tempfile kfw
import excel "./DataSkeleton/AssetClasses/PC1/raw_arb_basis.xlsx",sheet("KfW") firstrow clear
keep if date!=.
rename IB kfw
save `kfw', replace

tempfile kfw_bund_new
import excel "./DataSkeleton/AssetClasses/PC1/raw_arb_basis.xlsx",sheet("value") firstrow clear
destring C* G* US*, force replace
mmerge date using `kfw'
gen kfw_bund=100*(kfw-C9105)
mmerge date using "./DataSkeleton/AssetClasses/PC1/kfw_bund_update.dta"
replace kfw_bund=kfw_bund_update if date<td(14dec2011)
keep kfw_bund date
label var kfw_bund "KfW-bund spread" 
save `kfw_bund_new',replace

* TIPS-Treasury basis
tempfile inflation_swap_5y
import excel "./DataSkeleton/AssetClasses/PC1/raw_arb_basis.xlsx",sheet("value") firstrow clear
destring C* G* US*, force replace
keep date USSWIT
save `inflation_swap_5y',replace

tempfile treas5y
import excel "./DataSkeleton/AssetClasses/PC1/TreasuryRates_07122021.xlsx", sheet("Nominal") firstrow clear
keep Date I
rename I nom5y
destring nom5y, force replace
rename Date date
sort date
save `treas5y',replace

tempfile tips5y
import excel "./DataSkeleton/AssetClasses/PC1/TreasuryRates_07122021.xlsx", sheet("Real") firstrow clear
keep DATE YR
rename YR real5y
destring real5y, force replace
rename DATE date
sort date
save `tips5y',replace

tempfile tips_treas
use `inflation_swap_5y',clear
mmerge date using `treas5y',
mmerge date using `tips5y'
gen be=nom5y-real5y
gen tips_treas=100*(USSWIT5Curncy-be)
keep date tips_treas
label var tips_treas "TIPS-Treasury basis"
sort date
save `tips_treas',replace


*----------------------------------------------
* Merge everything together

use `cds_bond_basis.dta',clear
mmerge date using `cdx_basis.dta'
mmerge date using `tenor_basis.dta'
mmerge date using `swap_spread.dta'
mmerge date using `refco_treas.dta'
mmerge date using `kfw_bund_new.dta'
mmerge date using `tips_treas.dta'
gen bond_cds=-cds_bond
label var bond_cds "Bond-CDS basis"
gen neg_cdx_basis=-cdx_basis
label var neg_cdx_basis "CDS-CDX basis"
gen neg_swap_spread=-swap_spread
label var neg_swap_spread "Treasury-swap spread"
keep  date neg_cdx_basis neg_swap_spread bond_cds tenor_basis kfw_bund refco_treas tips_treas 
drop if date>td(31dec2020)
pca  neg_cdx_basis neg_swap_spread bond_cds tenor_basis kfw_bund refco_treas tips_treas 
predict pc1, score
keep if pc1!=.
label var pc1 "first PC of 7 arbitrage basis"
sort date 
save "./OutputInterim/clean_arbitrage_basis_final.dta",replace
outsheet using "./OutputInterim/clean_arbitrage_basis_final.csv",replace


*-----------------------------------------------------------------------

use `cds_bond_basis.dta',clear
mmerge date using `cdx_basis.dta'
mmerge date using `tenor_basis.dta'
mmerge date using `swap_spread.dta'
mmerge date using `refco_treas.dta'
mmerge date using `kfw_bund_new.dta'
mmerge date using `tips_treas.dta'
gen bond_cds=-cds_bond
label var bond_cds "Bond-CDS basis"
gen neg_cdx_basis=-cdx_basis
label var neg_cdx_basis "CDS-CDX basis"
gen neg_swap_spread=-swap_spread
label var neg_swap_spread "Treasury-swap spread"
mmerge date using "./OutputInterim/clean_arbitrage_basis_final.dta"
keep if pc1!=.
keep  date pc1 neg_cdx_basis neg_swap_spread bond_cds tenor_basis kfw_bund refco_treas tips_treas 
drop if date>td(31dec2020)
gen month=mofd(date)
sort date
collapse (lastnm) pc1 neg_cdx_basis neg_swap_spread bond_cds tenor_basis kfw_bund refco_treas tips_treas, by(month)
pca  neg_cdx_basis neg_swap_spread bond_cds tenor_basis kfw_bund refco_treas tips_treas  
predict pc1_month, score
format  month %tm
label var pc1 "m-end obs of daily PC1"
label var pc1_month "PC1 of m-end obs of daily aribtrages"
keep month pc1 pc1_month
save "./OutputInterim/clean_arbitrage_basis_pc1_monthly_final.dta",replace
outsheet using "./OutputInterim/clean_arbitrage_basis_pc1_monthly_final.csv",replace
